package javaSenior.threadExer;

/**
 * @author Lenl
 * @version v1.0
 * @create 2021-06-02 23:29
 * @description 使用同步方法实现线程安全
 */
public class WindowTest4 {
    public static void main(String[] args) {
        Window4 w1=new Window4();
        Window4 w2=new Window4();
        Window4 w3=new Window4();
        w1.setName("窗口1");
        w2.setName("窗口2");
        w3.setName("窗口3");
        w1.start();
        w2.start();
        w3.start();
    }
}

class Window4 extends Thread{
    private static int ticket=10000;
    private static Object obj=new Object();

    @Override
    public void run() {
        while (true) {
            if(!show()) break;
        }
    }

    //继承的得用静态的,使监视器为当前类
    private static synchronized boolean show(){
        if (ticket > 0) {
            System.out.println(Thread.currentThread().getName() + ":卖票，票号为：" + ticket);
            ticket--;
            return true;
        } else {
            return  false;
        }
    }
}